package com.suixun.service.credits.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suixun.dao.credits.CreditTaskArchiveMapper;
import com.suixun.entity.credits.CreditTaskArchive;
import com.suixun.enums.credits.CycleType;
import com.suixun.enums.credits.Status;
import com.suixun.enums.credits.UserRange;
import com.suixun.service.cache.CacheService;
import com.suixun.service.credits.CreditTaskArchiveService;

/**
 * 积分档案
 * 
 * @author yh
 * @date 2017年11月15日
 */
@Service
public class CreditTaskArchiveServiceImpl implements CreditTaskArchiveService {

    // 积分档案
    @Autowired
    private CreditTaskArchiveMapper taskArchiveMapper;
    
    // 缓存
    @Autowired
    private CacheService cacheService;

    @Override
    public void addOrUpdate(CreditTaskArchive cta) {
        if(cta.getId() == null){
            taskArchiveMapper.insert(insertEntity(cta));
        }else{
            taskArchiveMapper.updateByPrimaryKeySelective(insertEntity(cta));
        }
        cacheService.flushCreditTaskList();
    }

    /**
     * 创建默认任务实体
     * 
     * @param userId
     *            操作用户
     * @return
     */
    private CreditTaskArchive insertEntity(CreditTaskArchive cta) {
        cta.setStatus((short) Status.ENABLED.getValue());
        cta.setArchiveRange((short) UserRange.TEACHER.getValue());
        cta.setCycleType((short) CycleType.DAY.getValue());
        cta.setCreateTime(new Date());
        return cta;
    }

}
